package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Liu
 * @date: 2022/02/23
 * @time: 11:08
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /**
     * 用户登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        R<Employee> login = employeeService.login(employee);
        Employee data = login.getData();
        if (ObjectUtils.isEmpty(data)) {
            return R.error("登陆失败！");
        }
        request.getSession().setAttribute("employee", data.getId());
        return login;
    }

    /**
     * 用户退出
     *
     * @param request
     * @return
     */

    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {
        try {
            request.getSession().removeAttribute("employee");
            return R.success("退出成功！");
        } catch (Exception e) {
            return R.error("退出失败！");
        }

    }

    /**
     * 添加用户
     *
     * @param employee
     * @param request
     * @return
     */

    @PostMapping
    @Transactional
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request) {
        /*Long userId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(userId);
        employee.setCreateUser(userId);*/
        return employeeService.add(employee);
    }

    /**
     * 分页查询
     *
     * @param name
     * @param page
     * @param size
     * @return
     */

    @GetMapping("/page")
    public R<Page<Employee>> findById(String name, Long page, Long size) {
        return employeeService.findByPage(name, page, size);
    }

    /**
     * 修改状态/修改信息
     *
     * @param req
     * @param employee
     * @return
     */

    @PutMapping
    public R<String> updateStatus(HttpServletRequest req, @RequestBody Employee employee) {
        if (ObjectUtils.isEmpty(employee)) {
            return R.error("失败");
        }
        String username = employee.getUsername();
        String name = employee.getName();
        String phone = employee.getPhone();
        String sex = employee.getSex();
        String idNumber = employee.getIdNumber();
        R<String> res = null;
        if (ObjectUtils.isEmpty(username) && ObjectUtils.isEmpty(name) && ObjectUtils.isEmpty(phone) && ObjectUtils.isEmpty(sex) && ObjectUtils.isEmpty(idNumber)) {
            /*Long employee1 = (Long) req.getSession().getAttribute("employee");
            employee.setUpdateUser(employee1);*/
            return employeeService.updateStatus(employee);
        } else {
            return employeeService.updateInfo(employee);
        }
    }

    /**
     * 根据ID查找信息
     *
     * @param id
     * @return
     */

    @GetMapping("{id}")
    public R<Employee> findById(@PathVariable Long id) {
        return employeeService.findById(id);
    }
}
